import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class OrderService {

    private String address;


    public Order createOrder(OrderDishes[] orderDishes) throws NoAddressException {
        if (address == null) {
            throw new NoAddressException();
        }
        Order order = new Order();
        double totalAmount = 0d;
        for (OrderDishes orderDish : orderDishes) {
            double amounnt = orderDish.getDishes().getPrice() * orderDish.getCount();
            totalAmount = totalAmount + amounnt;
        }
        order.setTotalAmount(totalAmount);
        order.setDishes(orderDishes);
        return order;
    }

    public void confirmOrder(Order order) {
        System.out.println("本次下单的收获地址是:" + address);
        System.out.println("-----------");
        System.out.println("本次下单的商品信息是");
        System.out.println("-------");
        for (OrderDishes dish : order.getDishes()) {
            System.out.println("菜品名称:" + dish.getDishes().getName());
            System.out.println("菜品价格:" + dish.getDishes().getPrice() + "元");
            System.out.println("菜品数量:" + dish.getCount());
            System.out.println("-----");
        }
        System.out.println("本次需要支付的总金额是:" + order.getTotalAmount() + "元");
    }


    //通过判断输入进来的字符串是否能强转为double类，能就返回true，不能则返回false
    public static boolean isDouble(String str) {
        try {
            double num = Double.valueOf(str);
            return true;
        } catch (Exception e) {
            System.out.println("请重新输入");
            return false;
        }

    }

    public void payOrder(Order order) {
        boolean isTrue = false;

        while (!isTrue) {

            String str = null;

            try {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
                str = bufferedReader.readLine();
                isDouble(str);
            } catch (IOException e) {
                e.printStackTrace();
            }
            double amount = Double.valueOf(str);
            if (amount == order.getTotalAmount()) {
                isTrue = true;
                System.out.println("金额正确，订单已完成");
            } else {
                System.out.println("请重新输入");
            }
        }
    }


    public void setAddress(String address) {
        this.address = address;
    }

    public String getAddress() {
        return address;
    }
}
